<?php
    class binhchondb
    {
        public $_tbl_binhchon = 'giaphucvn_binhchon';
        public $_tbl_tuychonbinhchon = 'giaphucvn_tuychonbinhchon';
        public $_tbl_ketquabinhchon = 'giaphucvn_ketquabinhchon';
        
        public function GetPollId($MaBinhChon = 0)
		{
			$sql  = "select * from ".$this->_tbl_binhchon." where (1)";
            
            if($MaBinhChon != 0)
                $sql .= " AND MaBinhChon = $MaBinhChon";
			
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function GetMax()
		{
			$sql  = "select * from ".$this->_tbl_tuychonbinhchon." where (1) order by SapXep DESC limit 0,1";
			
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function Add($TenTuyChon = "", $MaBinhChon = 0, $SapXep = 0, $DuocChon = 0)
        {
            $sql = "insert into ".$this->_tbl_tuychonbinhchon."(TenTuyChon, MaBinhChon, SapXep, DuocChon) values('".$TenTuyChon."',".$MaBinhChon.",".$SapXep.",".$DuocChon.")";
            
            echo $sql;
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
        }
        
        public function Update($MaBinhChon = 0, $TieuDe = "")
		{
			$sql  = "update ".$this->_tbl_binhchon." Set TieuDe = '$TieuDe' where (1)";
            
            if($MaBinhChon != 0)
                $sql .= " AND MaBinhChon = $MaBinhChon";
			
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
        
        public function DeleteOption($MaTuyChon = 0)
		{
            $sql  = "DELETE FROM ".$this->_tbl_tuychonbinhchon." WHERE (1)";
            
            if($MaTuyChon != 0)
                $sql .= " AND MaTuyChon = $MaTuyChon";
			
            $db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
        
        public function DeleteVote($MaTuyChon = 0)
		{
            $sql  = "DELETE FROM ".$this->_tbl_ketquabinhchon." WHERE (1)";
            
            if($MaTuyChon != 0)
                $sql .= " AND MaTuyChon = $MaTuyChon";
            
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
        
        public function GetPollOption($MaBinhChon = 0)
		{
			$sql  = "select * from ".$this->_tbl_tuychonbinhchon." where (1)";
            
            if($MaBinhChon != 0)
                $sql .= " AND MaBinhChon = $MaBinhChon";
                
            $sql .= " order by SapXep";
            
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
         public function GetPollVotes($MaTuyChon = 0)
		{
			$sql  = "select * from ".$this->_tbl_ketquabinhchon." where (1)";
            
            if($MaTuyChon != 0)
                $sql .= " AND MaTuyChon = $MaTuyChon";
               
			$db = new dataservice();
            return $db->ReturnNumRows($sql);
		}

        public function UpdatePoll($MaTuyChon = 0, $DiaChiIp = "")
        {
            $sql = "insert into ".$this->_tbl_ketquabinhchon."(MaTuyChon, DiaChiIp) values(".$MaTuyChon.",'".$DiaChiIp."')";
            
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
        }
    }
?>